package service

import (
	"e_mall_server/dao/mysql"
	"e_mall_server/models"
	"errors"
	"github.com/gin-gonic/gin"
)

/**
 * @Author: zhaobin
 * @Description TODO
 * @Date: 2022-05-18 18:12
 */

// GetCartList 获取购物车列表接口和总价
func GetCartList(c *gin.Context) (models.CartResponse, error) {
	var cartList []models.Cart

	// 需要判断是否登陆，登陆则通过redis获取

	// 获取购物车列表
	models.Cookie.Get(c, models.CartList, &cartList)

	return getCartMessage(cartList, nil)
}

// GetCheckedCartList 获取中的购物车列表接口和总价
func GetCheckedCartList(c *gin.Context) (models.CartResponse, error) {

	// 需要判断是否登陆，登陆则通过redis获取

	// 设置购物车列表
	var cartList []models.Cart
	models.Cookie.Get(c, models.CartList, &cartList)

	var orderList []models.Cart
	for i := 0; i < len(cartList); i++ {
		if cartList[i].Checked {
			orderList = append(orderList, cartList[i])
		}
	}

	return getCartMessage(orderList, nil)
}

// AddCart 添加购物车信息
func AddCart(c *gin.Context, goodsId int64) (models.CartResponse, error) {

	// 判断是否已经登陆
	// 如果已经登陆则需要存储到redis中
	// 没有登陆，保存购物车信息到cookie中

	// 1 获取增加购物车的数据  （把哪一个商品加入到购物车）
	goods := mysql.GetGoodsById(goodsId)
	cart := models.Cart{
		GoodsId:  goodsId,
		Title:    goods.Title,
		Price:    goods.Price,
		Num:      1,
		GoodsImg: goods.GoodsImg,
		Checked:  true, /*默认选中*/
	}
	// 2 判断购物车有没有数据   （cookie）
	var cartList []models.Cart

	models.Cookie.Get(c, models.CartList, &cartList)

	if len(cartList) > 0 {
		// 4 如果购物车有数据
		if models.HasCartData(cartList, cart) {
			// 4.1 有当前数据让当前数据的数量加1，然后写入到cookie
			for i := 0; i < len(cartList); i++ {
				if cartList[i].GoodsId == cart.GoodsId {
					cartList[i].Num++
				}
			}
		} else {
			// 4.1 如果没有当前数据直接写入cookie
			cartList = append(cartList, cart)
		}
	} else {
		// 3 如果购物车没有任何数据  直接把当前数据写入cookie
		cartList = append(cartList, cart)
	}
	models.Cookie.Set(c, models.CartList, cartList)

	return getCartMessage(cartList, nil)
}

// UpdateCartNum 修改购物车信息
func UpdateCartNum(c *gin.Context, updateCartNumParam *models.UpdateCartNumParam) (models.CartResponse, error) {
	var cartList []models.Cart
	models.Cookie.Get(c, models.CartList, &cartList)
	if len(cartList) > 0 {
		for i := 0; i < len(cartList); i++ {
			if cartList[i].GoodsId == updateCartNumParam.GoodsId {
				cartList[i].Num = updateCartNumParam.Num
				// 把商品状态改为选中
				cartList[i].Checked = true
			}
		}
		//重新写入数据
		models.Cookie.Set(c, "cartList", cartList)

	} else {
		return getCartMessage(cartList, errors.New("传入参数错误"))
	}
	return getCartMessage(cartList, nil)
}

// UpdateCartChecked 修改购物车商品是否选中
func UpdateCartChecked(c *gin.Context, updateCartCheckedParam *models.UpdateCartCheckedParam) (models.CartResponse, error) {
	var cartList []models.Cart
	models.Cookie.Get(c, models.CartList, &cartList)
	if len(cartList) > 0 {
		for i := 0; i < len(cartList); i++ {
			if cartList[i].GoodsId == updateCartCheckedParam.GoodsId {
				// 更改商品状态
				cartList[i].Checked = updateCartCheckedParam.Checked
			}
		}
		//重新写入数据
		models.Cookie.Set(c, "cartList", cartList)
	} else {
		return getCartMessage(cartList, errors.New("传入参数错误"))
	}
	return getCartMessage(cartList, nil)
}

// UpdateCartAllChecked 修改购物车商品是否选中
func UpdateCartAllChecked(c *gin.Context, updateCartAllCheckedParam *models.UpdateCartAllCheckedParam) (models.CartResponse, error) {
	var cartList []models.Cart
	models.Cookie.Get(c, models.CartList, &cartList)
	if len(cartList) > 0 {
		for i := 0; i < len(cartList); i++ {
			// 更改商品状态
			cartList[i].Checked = updateCartAllCheckedParam.AllChecked
		}
		// 重新写入数据
		models.Cookie.Set(c, "cartList", cartList)
	} else {
		return getCartMessage(cartList, errors.New("传入参数错误"))
	}
	return getCartMessage(cartList, nil)
}

// DeleteCartGoods 删除购物车商品接口
func DeleteCartGoods(c *gin.Context, deleteCartGoodsParam *models.DeleteCartGoodsParam) (models.CartResponse, error) {
	var cartList []models.Cart
	models.Cookie.Get(c, models.CartList, &cartList)
	if len(cartList) > 0 {
		for i := 0; i < len(cartList); i++ {
			if cartList[i].GoodsId == deleteCartGoodsParam.GoodsId {
				// 删除商品信息
				cartList = append(cartList[:i], cartList[(i+1):]...)
			}
		}
		//重新写入数据
		models.Cookie.Set(c, "cartList", cartList)
	} else {
		return getCartMessage(cartList, errors.New("传入参数错误"))
	}
	return getCartMessage(cartList, nil)
}

// GetCartDetailById 通过购物车id获取购物车详细信息
func GetCartDetailById(id int64) (models.Cart, error) {
	return mysql.GetCartDetailById(id)
}

// 通过购物车列表返回购物车的相关信息
func getCartMessage(cartList []models.Cart, err error) (models.CartResponse, error) {
	cartResponse := models.CartResponse{}
	// 设置购物车总价
	var allPrice float64
	// 商品数量
	cartCount := 0
	// 选中的商品数量
	cartCheckCount := 0

	for i := 0; i < len(cartList); i++ {
		if cartList[i].Checked {
			allPrice += cartList[i].Price * float64(cartList[i].Num)
			cartCheckCount += cartList[i].Num
		}
		cartCount += cartList[i].Num
	}

	cartResponse.CartList = cartList
	cartResponse.AllPrice = allPrice
	cartResponse.CartNum = cartCount
	cartResponse.CartCheckedNum = cartCheckCount

	return cartResponse, err
}
